import {
  getGame,addClick,addOpen,getForum
} from "../../api/getApi"


Page({

  data: {
    swiperGame:[],
    cainiGame:[],
    hotGame:[],
    forumData:[],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getGame({page:1,limit:10,randomGame:'true'}).then(res => {
      this.setData({cainiGame:res.list})
      console.log(res)
    })
    getForum({page:1,limit:5,randomGame:"true"}).then(res => {
      res.list.forEach(element => {
        if(element.image){
          element.image =element.image.split(",")
        }
      });
      this.setData({forumData:res.list})
    })
    getGame({page:1,limit:10,randomGame:'true',bannerGame:'true'}).then(res => {
      const list = res.list;
      if (list && list.length > 0) {
        this.setData({
            swiperGame: list.slice(0, 5)
        });
        this.setData({
            hotGame: list.slice(5,9)
        });
      }
    })
  },

  goToGame(e){
    let itemdata = e.currentTarget.dataset.data
    if (itemdata.appid){
      wx.navigateToMiniProgram({
        appId: itemdata.appid, 
        path: '?wxgamepro=CpsCBgAAoXkpQY9UXS9__OwV',
        envVersion: 'release',
        success:(res)=>{
          let myGame = wx.getStorageSync('myGame')
          if(!myGame){
            myGame = []
          }else{
            myGame = JSON.parse(myGame).slice(0, 300)
          }
          if(!myGame.find(v=>v.id===itemdata.id)){
            myGame=[itemdata,...myGame]
          }
          wx.setStorageSync('myGame', JSON.stringify(myGame))
          this.setData({myGame:myGame})
          addOpen({id:itemdata.id})
        }
      })
      addClick({id:itemdata.id})
    }else{
      wx.showToast({
        title: `尊敬的大人对对不不起起！此游戏出了点小问题~`,
        icon: 'none',
      });
    }
  },
  goToForum(e){
    let data = e.currentTarget.dataset.data
    wx.setStorageSync('forumType', data)
    wx.navigateTo({
      url: '/pages/forum1/forum1',
    })
  },
  goToForumDetail(e){
    let data = e.currentTarget.dataset.data
    wx.setStorageSync('forumDetail', JSON.stringify(data))
    wx.navigateTo({
      url: '/pages/forumDetail/forumDetail',
    })
  },
  goToAll(e){
    let data = e.currentTarget.dataset.data
    if(data=="1"){
      wx.navigateTo({
        url: '/pages/myGame/myGame',
      })
    }
    if(data=="2"){
      wx.setStorageSync('tabIndex', 1)
      wx.setStorageSync('tab2Index', 2)
      wx.switchTab({
        url: '/pages/type/type',
      })
    }
    if(data=="3"){
      wx.setStorageSync('tabIndex', 1)
      wx.setStorageSync('tab2Index', 0)
      wx.switchTab({
        url: '/pages/type/type',
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let myGame = wx.getStorageSync('myGame')
    if(!myGame){
      myGame = []
    }else{
      myGame = JSON.parse(myGame).slice(0, 20)
    }
    this.setData({myGame:myGame})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
   getGame({page:1,limit:10,randomGame:'true'}).then(res => {
      this.setData({cainiGame:res.list})
      console.log(res)
    })
    getGame({page:1,limit:10,randomGame:'true',bannerGame:'true'}).then(res => {
      const list = res.list;
      if (list && list.length > 0) {
        this.setData({
            swiperGame: list.slice(0, 5)
        });
        this.setData({
            hotGame: list.slice(5,9)
        });
      }
    })
    let myGame = wx.getStorageSync('myGame')
    if(!myGame){
      myGame = []
    }else{
      myGame = JSON.parse(myGame).slice(0, 20)
    }
    this.setData({myGame:myGame})
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})